/*
* Copyright (c) 2011 Mladen Milankovic <mmlado at gmail.com>.
*
* This file is part of Sink Submarines application
*
* Sink Submarines is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sink Submarines is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with Sink Submarines.  If not, see <http://www.gnu.org/licenses/>.
*/

import QtQuick 1.0

import "core"

Rectangle {
    id: main
    height: 480
    width: 800
    property bool accelerometer: false
    property bool proximity: false
    Item {
        id: shipLayer
        height: parent.height * 0.1
        width: parent.width
        Ship {
            id: ship
        }
    }

    Item {
        id: bombLayer
        function addBomb() {
            //console.debug(this.children.length);
            if (this.children.length < 4) {
                var component = Qt.createComponent("core/Bomb.qml");
                if (component.status == Component.Ready) {
                    component.createObject(bombLayer);
                }
            }
        }
    }

    MouseArea {
        anchors.fill: parent
        hoverEnabled: true
        onMousePositionChanged: {
            if (accelerometer) return;
            if ((mouse.x > 0 + (ship.width/2)) && (mouse.x < (main.width - (ship.width/2)))) {
                ship.x = mouse.x - (ship.width/2);
            }
        }
        onClicked: {
            if (proximity) return;
            bombLayer.addBomb();
        }
    }

    function initialize() {
        var co = Qt.createComponent("core/Accelerometer.qml");
        if (co.status == Component.Ready) {
            var o = co.createObject(main);
            main.accelerometer = true;
            o.start();
        }
        var co = Qt.createComponent("core/Proximity.qml");
        if (co.status == Component.Ready) {
            var o = co.createObject(main);
            main.proximity = true;
            o.start();
        }
    }

    Component.onCompleted: initialize()

}
